/*
 * Copyright (C) 2011 Everybody all the Time
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.everybodyallthetime.android.example.colorpickerpreference;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import com.everybodyallthetime.android.dialog.colorpicker.ColorPickerDialogPreference;
import com.everybodyallthetime.android.example.colorpickeractivity.R;

import java.util.HashMap;
import java.util.Map;

public class SamplePreferenceActivity extends PreferenceActivity {

    //default values just in case you have multiple color pickers
    private Map<String, Integer> COLOR_PREFERENCES;
    private void initializeMaps() {
        COLOR_PREFERENCES = new HashMap<String, Integer>() {
            {
                put("color_picker", 1827005926);
            }
        };
    }

    SharedPreferences mSharedPreferences;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        addPreferencesFromResource(R.xml.preferences);
        //initialize the color picker key/default map
        initializeMaps();
    }


    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        super.onPreferenceTreeClick(preferenceScreen, preference);
        String key = preference.getKey();

        // if this is a color preference
        if (isColorPreference(key)) {

            // handle the click by opening the color picker dialog
            ColorPickerDialogPreference cp = new ColorPickerDialogPreference(this, colorChanged, preference, mSharedPreferences.getInt(key, COLOR_PREFERENCES.get(key)));
            cp.show();
        }

        return false;

    }

    /**
     * returns true if the preference is in the color picker preference, COLOR_PREFERENCES
     *
     * @param key
     * @return
     */
    private boolean isColorPreference(String key) {

        if (COLOR_PREFERENCES.containsKey(key))
            return true;
        else
            return false;

    }


    /**
     * handler for color changed
     */
    ColorPickerDialogPreference.OnPreferenceColorChangedListener colorChanged = new ColorPickerDialogPreference.OnPreferenceColorChangedListener() {

        public void colorChanged(Preference preference, int color) {

            mSharedPreferences.edit().putInt(preference.getKey(), color).commit();
            findPreference(preference.getKey()).setSummary("Color: " + color);
        }
    };

}
